<template>
  <el-dialog
    :title="$t('message.add')"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="150px">
      <el-form-item :label="$t('message.dealer')" prop="creatorId" :class="{ 'is-required': true }">
        <el-select v-model="dataForm.creatorId">
          <el-option v-for="item in userList" :key="item.userId" :label="item.company" :value="item.userId" />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('message.serialNo')" prop="serialNo" :class="{ 'is-required': true }">
        <el-input v-model="dataForm.serialNo" :placeholder="$t('message.serialNo')"></el-input>
      </el-form-item>
      <el-form-item :label="$t('message.type')" prop="type" :class="{ 'is-required': true }">
        <el-radio-group v-model="dataForm.type">
          <el-radio :label="1">{{$t('message.unlimited')}}</el-radio>
          <el-radio :label="2">{{$t('message.limited')}}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item :label="$t('message.surplusTimes')" prop="remainTimes" :class="{ 'is-required': true }">
          <el-input-number v-model="dataForm.remainTimes" controls-position="right" :min="0" :label="$t('message.surplusTimes')" :disabled="dataForm.type === 1"></el-input-number>
      </el-form-item>
      <!-- <el-form-item :label="$t('message.status')" size="mini" prop="status" :class="{ 'is-required': true }">
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="0">{{$t('message.disable')}}</el-radio>
          <el-radio :label="1">{{$t('message.ok')}}</el-radio>
        </el-radio-group>
      </el-form-item> -->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">{{$t('message.cancle')}}</el-button>
      <el-button type="primary" @click="dataFormSubmit()">{{$t('message.submit')}}</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dialogVisible: false,
        userList: [],
        dataForm: {
          serialNo: '',
          type: 1,
          status: 1,
          remainTimes: 0,
          creatorId: ''
        },
        dataRule: {
          serialNo: [
            { required: true, message: this.$t('message.noEmpty'), trigger: 'blur' }
          ],
          type: [
            { required: true, message: this.$t('message.noEmpty'), trigger: 'blur' }
          ],
          remainTimes: [
            { required: true, message: this.$t('message.noEmpty'), trigger: 'blur' }
          ],
          creatorId: [
            { required: true, message: this.$t('message.noEmpty'), trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init () {
        this.visible = true
        if (this.$refs['dataForm'] !== undefined) {
          this.$refs['dataForm'].resetFields()
        }
        this.$http({
          url: this.$http.adornUrl(`/sys/device/queryTotalSysUser`),
          method: 'post',
          data: this.$http.adornData()
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.userList = data.userList
          } else {
            this.$message.error(data.msg)
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/device/saveDeviceInfo`),
              method: 'post',
              data: this.$http.adornData({
                'serialNo': this.dataForm.serialNo,
                'status': this.dataForm.status,
                'type': this.dataForm.type,
                'remainTimes': this.dataForm.remainTimes,
                'creatorId': this.dataForm.creatorId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: this.$t('message.success'),
                  type: 'success',
                  duration: 500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
